#!/bin/bash

######################################################################################
#
#                     Script of  Generation of FCC FR source 
#    The default orientation of cell is
#                      | Normalize[{ 1,-1, 0}] |
#                      | Normalize[{ 1, 1, 1}] |
#                      | Normalize[{-1,-1, 2}] |
#    which is matrix orientation of PBC polycrystal with twin. 
#    if you want to change the orientation of simulation cell, you can use follwing
#    two ways:
#      (1)  <-xMatDir ux,uy,uz -yMatDir vx,vy,vz> or
#           <-zMatDir wx,wy,wz -yMatDir vx,vy,vz> or
#           <-zMatDir wx,wy,wz -xMatDir ux,uy,uz> 
#           we just need two directions of axis.
#      (2)  <-TBAngle angle(radian system)>
#           TBAngle is the angle between Z axis and normal of ABC glide plane, if
#	    twin exist, the normal of ABC represent TB plane. Besides, the another
#           two independ variables in rotate matrix is random.
#    if the above two ways are both in command line, first one is default.
#    $1 is the type of simulation, $2 is randm seed.
#                    
######################################################################################


#frlen=1800
frlen=60
cubel=2000
#frlen=1980
#cubel=2000
maxSeg=133
nfrs=8
maxDensity=1E-6
#seed="-seed 1566393598"
polycrystal=1
single=2

PI=3.1415926535897932385
if [ ${polycrystal} == "1" ]; then
    srctype=5
#    angle=`echo "scale=16; ${1}.0 * ${PI} / 180.0 " | bc -l`
else
    srctype=2
    if [ ${single} = "1" ]; then
        lnum=`grep  -n "basis orientation:" ${1} | cut -d : -f 1`
    else
        lnum=`grep  -n "twin orientation:" ${1} | cut -d : -f 1`
    fi

    let lnum=${lnum}+1
    l1=`sed -n "${lnum}p" ${1}`
    x1=`echo ${l1} | cut -d " " -f 2 `
    x2=`echo ${l1} | cut -d " " -f 3 `
    x3=`echo ${l1} | cut -d " " -f 4 | cut -d "}" -f 1`
    let lnum=${lnum}+1
    l2=`sed -n "${lnum}p" ${1}`
    y1=`echo ${l2} | cut -d " " -f 2 `
    y2=`echo ${l2} | cut -d " " -f 3 `
    y3=`echo ${l2} | cut -d " " -f 4 | cut -d "}" -f 1`

    xdir=`echo "${x1}${x2}${x3}"`
    ydir=`echo "${y1}${y2}${y3}"`
fi
case $srctype in
######################################################################################
#
#    Type I: oringin code, PBC
#
######################################################################################
    1|I)
    ./bin/paradisgen -type 4  -cubel ${cubel} -maxseg ${maxSeg} -nfrsrcs ${nfrs}  \
    -polycrystal 0  -pbc 0 -extend 0  -srcLenType 0 \
    -xMatDir 1,0,0 -zMatDir 0,0,1 ;;

#    -polycrystal 0 -pbc 0 \
######################################################################################
#
#    Type II: free surface, single crystal, gradient sources distribution
#
######################################################################################
    2|II)
    ./bin/paradisgen -type 3 -frlen ${frlen} -cubel ${cubel} -maxseg ${maxSeg} -nfrsrcs ${nfrs} $seed \
    -polycrystal 0 -pbc 7 \
    -xMatDir ${xdir} -yMatDir ${ydir} -maxDisDen ${maxDensity} \
    -heightRatio 2 -deltaNums 0 -nLayers 1 ;;

#    -xMatDir 1,0,0 -yMatDir 0,1,0 \
#    -polycrystal 0 -pbc 7 -extended 4\

######################################################################################
#    
#    Type III: polycrystal, only have grain boundaries.
#    ./bin/paradisgen -type 3 -frlen 2000 -cubel 4000 -maxseg 100 -nfrsrcs 10 $seed \
#    -polycrystal 1 -nGrain 1 -pbc 0 \
#    -xMatDir 1,0,0 -yMatDir 0,1,0 ;; 
#    
######################################################################################
    3|III)
    echo "can not suppose this type" 
    exit 1 ;;

######################################################################################
#    
#    Type IV: polycrystal with twin, PBC, simulate RVE
# 
######################################################################################
    4|IV)
    ./bin/paradisgen -type 3 -frlen 1500 -cubel 4000 -maxseg 100 -nfrsrcs 1 $seed \
    -polycrystal 1 -nGrain 4 -twinRatio 0.5 -pbc 0 ;;

######################################################################################
#    
#    Type V: bicrystal with twin boundary, simulate sample
#
######################################################################################
    5|V)

    ./bin/paradisgen -type 3 -frlen ${frlen} -cubel ${cubel} -maxseg ${maxSeg} -nfrsrcs ${nfrs} $seed \
    -polycrystal 1 -nGrain 2 -pbc 7 -maxDensity $maxDensity \
    -voroFile input/Copper.vor \
    -twinRatio 0.5 -heightRatio 2 -deltaNum 0 -nLayers 1 ;;
#    -TBAngle ${angle} -twinRatio 0.5 -heightRatio 2 -deltaNum 0 -nLayers 1 -out SAS/${2} > /dev/null 2>&1;;

#    -xMatDir ${xdir} -yMatDir ${ydir} -twinRatio 0.5 -heightRatio 2 -deltaNum 0 -nLayers 1;;
#    -xMatDir 0.707106781186548,-0.707106781186548,0.0 -yMatDir 0.642228525188087,0.642228525188087,-0.418431646591735 \
    *)
     echo "input error!"
     exit 1 ;;
esac

./bin/paradisrepart -infile paradis.data -cells 4 -domains 2,2,1 -outfile input/Copper.data > /dev/null 2>&1
